export function toast(title) {
    uni.showToast({
        title,
        icon: 'none',
        duration: 2000
    })
}
//    获取当前【年月日时分秒】
export function countCurrent() {
    let date = new Date();
    let year = date.getFullYear() // 年
    let month = date.getMonth() + 1; // 月
    let day = date.getDate(); // 日
    let hour = date.getHours(); // 时
    let minutes = date.getMinutes(); // 分
    let seconds = date.getSeconds() //秒
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (day >= 0 && day <= 9) {
        day = "0" + day;
    }
    if (hour >= 0 && hour <= 9) {
        hour = "0" + hour;
    }
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
    }
    if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds;
    }
    // clearInterval(timer);
    return year + "." + month + "." + day + " " + hour + ":" + minutes + ":" + seconds;
}

export function countdown(time, timer) {
    const day = parseInt(time / (60 * 60) / 24)
    const hour = parseInt(time / (60 * 60) % 24) //parseInt(time / (60 * 60))
    const minute = parseInt((time % (60 * 60)) / 60)
    const second = +((time % (60 * 60)) % 60).toFixed()
    let result = {
        day: day > 9 ? day : `0${day}`,
        hour: hour > 9 ? hour : `0${hour}`,
        minute: minute > 9 ? minute : `0${minute}`,
        second: second > 9 ? second : `0${second}`
    }
    if (hour <= 0 && minute <= 0 && second <= 0) {
        console.log('清除l');
        clearInterval(timer);
    }
    return result
}
/*
   res :  结束时间戳 - 开始时间戳       
   timer ：定时器
*/
export function timestampToTime(res, timer) {
    // let day = 
    let hours = parseInt((res % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    let minutes = parseInt((res % (1000 * 60 * 60)) / (1000 * 60));
    let seconds = (res % (1000 * 60)) / 1000;
    hours = hours < 10 ? '0' + hours : hours;
    minutes = minutes < 10 ? '0' + minutes : minutes;
    seconds = seconds < 10 ? '0' + seconds : seconds;
    let miao = parseInt(seconds);
    if (miao < 10) {
        miao = '0' + miao;
    }
    let result = hours + ':' + minutes + ':' + miao;
    if (hours <= 0 && minutes <= 0 && miao <= 0) {
        miao = '0' + miao;
        console.log('清除l');
        clearTimeout(timer);
    }
    return result
}

//    请求并发   ES6
export const concurrencyRequest = (callBack = []) => {
    return Promise.all([...callBack]).then(res => {
        return res
    })
}
//    请求并发   ES7
export let func = async (callback = []) => {
    let arr = []
    for (let i = 0; i < callback.length; i++) {
        arr.push(await callback[i])
    };
    return arr
}
//   数字千位分隔符且保留两位小数
/*
   value : number    
   return: string  '1,000.00'
*/
export function ThousandAndDecimal(num) {
    return num ? (num.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')) : ('0.00')
}
//   判断 H5环境
export let getEnv = () => {
    let env = location.hostname.toLowerCase(),
        port = location.port;
    let serviceUrl = null;
    let envVersion = {
        'development': 'https://hzhb.youyida-test.cn', // 测试环境
        'production': {
            "trial": 'https://hzhb.youyida-test.cn', // 测试环境
            "release": 'https://hzhb.youyida.com', // 生产环境
        }
    };
    const ENV = process.env.NODE_ENV;
    //    本地 打包
    if (port && ENV === 'production') {
        serviceUrl = envVersion['development']
        return {
            serviceUrl
        };
    }
    //   线上打包 test prd
    if (ENV === 'production') {
        serviceUrl = env.includes('test') ? envVersion[ENV]["trial"] : (envVersion[ENV]["release"]);
    }
    //  dev
    if (ENV === 'development') serviceUrl = envVersion[ENV];
    return {
        serviceUrl
    }
}
export let singleSignOn = () => {
    return new Promise((resolve, reject) => {
        uni.login({
            provider: getApp().globalData.provider,
            success: async loginRes => {
                resolve(loginRes)
            },
            fail: async loginRes => {
                reject(loginRes)
            }
        })
    })
}
export let isValidPhoneOrLandline = (phone) => {
    return (/^[1][3-9][0-9]{9}$/.test(phone) || (/^0(\d{2,3})(-?)(\d{7,8})$/).test(phone));
}